#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdarg.h>
#include <sys/time.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdarg.h>
#include <getopt.h>
#include "core.h"
#include "../utils/longtime.h"
#include "utils_mtrace.h"

#define SYSLOG_FILE 		"./sysrun.log"
#define PID_FILE 			"/var/tmp/.pid_mten.dat"
static void *log_handle = NULL;
static void *log_trans = NULL;
static void *log_mq = NULL;
static void *log_sys = NULL;
static int enable_reboot = 1;

void sys_log(const char *fmt, ...)
{
    va_list va;
    int rc;
    char str[4096] = {0};
    if (log_sys == NULL)
        return;
    va_start(va, fmt);
    rc = mtrace_vlog(log_sys, fmt, va);
    va_end(va);
}

void lprintf(const char *fmt, ...)
{
	va_list va;
	int rc;
	char str[4096] = {0};
	if (log_handle == NULL)
		return;
	va_start(va, fmt);
	rc = mtrace_vlog(log_handle, fmt, va); 
	va_end(va);

	
	va_start(va, fmt);
	vsnprintf(str, sizeof(str), fmt, va);
	va_end(va);
	printf(str);
}

void trace_log(const char *fmt, ...)
{
	va_list va;
	int rc;
	char str[4096] = {0};
	if (log_trans == NULL)
		return;
	va_start(va, fmt);
	rc = mtrace_vlog(log_trans, fmt, va); 
	va_end(va);
	
	va_start(va, fmt);
	vsnprintf(str, sizeof(str), fmt, va);
	va_end(va);
	printf(str);
}

void log_mqtt(const char *fmt, ...)
{
    va_list va;
    int rc;
    char str[4096] = {0};
    if (log_mq == NULL)
        return;
    va_start(va, fmt);
    rc = mtrace_vlog(log_mq, fmt, va);
    va_end(va);
}



static void check_time()
{
	struct timeval tv;
	struct tm *ts;
	struct tm tc;
	gettimeofday(&tv, NULL);
	time_t tt = tv.tv_sec;
	ts = localtime(&tt);
	if (ts->tm_year < (2018 - 1900))
	{
		strptime("2019-01-28 12:12:12", "%F %H:%M:%S", &tc);
		tv.tv_sec = mktime(&tc);
		tv.tv_usec = 0;
		settimeofday(&tv, NULL);
	}
}

#define true 1
#define false 0
int main(int argc, char *const argv[])
{
	int opt;
	int b_stop = false;
	int b_daemon = false;
	check_time();
	char log_name[100] = "/var/log";

	// parse run time argumnets
	while ((opt = getopt(argc, argv, ":drxb")) != -1)
	{
		switch (opt)
		{
		case ':':
			printf("missing required parameter for command option %c.\n", opt);
			return -1;
		case '?':
			printf("unknown option %c, ignored\n", optopt);
			break;
		case 'b':
			printf("disable auto reboot function\r\n");
			enable_reboot = 0;
			break;
		case 'd':
		case 'r':
			b_daemon = true;
			break;
		case 'x':
			b_stop = true;
			break;
		}
	}
	log_handle = mlog_init(log_name, "mten_sys");
	mlog_setlimitcnt(log_handle, 200, 2);

	log_trans = mlog_init(log_name, "mten_operation");
	mlog_setlimitcnt(log_trans, 200, 2);

	daemon_config("mten", PID_FILE, lprintf);
	daemon_service(service_run, service_stop);

	if (b_stop)
	{
		printf("Stop daemon and it's child process...\n");
		daemon_stop();
		return 0;
	}
	else if (daemon_probe() == 0)
	{
		printf("another copy is running, please stop it first by \"%s -x\"\n", argv[0]);
		return 0;
	}

	if (b_daemon)
		daemon_main();
	else
		service_run();
	return 0;
}
